package com.android.music.prolyric;


import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URI;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;

import com.android.music.prolyric.database.DatabaseManger;
import com.android.music.prolyric.database.LyricDTO;
import com.android.music.prolyric.search.ConnectionDetector;
import com.android.music.prolyric.search.SearchLyric;

import android.app.IntentService;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;

public class LoadLyricService extends IntentService{
	 static InputStream is = null;
	 static JSONObject jObj = null;
	 static String json = "";
	 DatabaseManger data ;	   
	 private static final  String urlIndex = "http://loidich.com/index.php";
	 private ConnectionDetector cd;
	 
	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		super.onCreate();
		data = new DatabaseManger(getBaseContext());
		cd = new ConnectionDetector(getBaseContext());
	}
	
	public LoadLyricService() {
		super("");
	}
	

	public LoadLyricService(String name) {
		super(name);
		
	}
	
	
	

	@Override
	protected void onHandleIntent(Intent intent) {
		Log.i("bat dau tim", "ok");
		String[] lycics = null;
		SearchLyric search = new SearchLyric();
		String songname = intent.getExtras().getString("songname");
		String singer = intent.getExtras().getString("singer");
		String urlSong = intent.getExtras().getString("urlSong");
		int lyricID = intent.getExtras().getInt("lyricID");
		Log.d("LoadLyricService_lyricID", lyricID+"");
		int localSearch = 0 ;
		String singerSearch = "";
		if(singer!=null && singer.indexOf("unknow")>0)
			singerSearch = null;
		else 
			singerSearch = singer;
		
		//Log.d("singer", singer);
		/*
		 * Thu tu tim kiem : 
		 * 1: tim trong co so du lieu'
		 * 2: tim theo urlSong
		 * 3: tim theo ten bai hat
		 * */
	
		
		ArrayList<HashMap<String, String>> lyricResult = null;
		
		if(urlSong != null)
		{
			if(songname!= null)
			{
				
				lyricResult = data.getLyricByName(songname, singer);
				if(lyricResult == null)
				{
					localSearch = 1;
					if(cd.isConnectingToInternet())
					{
						lyricResult = data.getLyricFromHost(songname, singerSearch);
						if(lyricResult == null)
						{
							Log.d("tim tren web ", "tim tren web");
							localSearch = 2; 
							try {
								lyricResult = search.getLyricFromURL(urlSong);
								Log.i("hello", "hello");
							} catch (Exception e) {
								// TODO: handle exception
							}
						}
					}					
					
				}
				
			}
			else 
			{
				localSearch = 2; 
				try 
				{
					if(cd.isConnectingToInternet())
						lyricResult = search.getLyricFromURL_online(urlSong);
				} catch (Exception e) {
					// TODO: handle exception
				}
			}
			
			
		
		}
		else if(songname != null)
		{
			if(singer!=null)
				lyricResult = data.getLyricByName(songname, singer);			
			if(lyricResult == null)
			{
				localSearch = 1;
				if(cd.isConnectingToInternet())
				{
					if(singerSearch!=null)
						lyricResult = data.getLyricFromHost(songname, singerSearch);
					
					if(lyricResult == null)
					{
						Log.d("tim tren web ", "tim tren web");
						localSearch = 2; 
						try {
							lyricResult =search.getLyricByName(songname, singerSearch);
						} catch (Exception e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				}
			}
		}
		
		
		
		
		
		//gui du lieu ve
		Intent broadcastIntent = new Intent();
		broadcastIntent.setAction("lOAD_LYRIC_FROM_WEB");
		Bundle b = new Bundle();
		b.putSerializable("lyricLoader", lyricResult);
		b.putInt("lyricID", lyricID);
		b.putInt("localSearch", localSearch);
		broadcastIntent.putExtras(b);
		getBaseContext().sendBroadcast(broadcastIntent);
		
		
	}
	
	
	/*if(urlSong!=null && songname!= null)
	{
		Log.d("urlSong,songname", urlSong+"  " + songname);
	}

	if(urlSong!=null)
	{
		if(songname != null)
		{
			//Kiem tra neu co songname thi tim trong csdl truoc
			lyricLoaded = data.getLyricByName(songname,singer);
			if(lyricLoaded!=null)
			{
				
				lycics = new String [2];
				lycics[0]= lyricLoaded.getLyricEn();
				lycics[1]= lyricLoaded.getLyricVi();
				
			}
			else // tim theo urlSong
			{


				
				lyricLoaded = data.getLyricFromHost(songname,singer);
				if(lyricLoaded != null)
				{
					localSearch=1;
					Log.d("LoadLyricFromHost", "have lyric from host");
					lycics = new String [2];
					lycics[0]= lyricLoaded.getLyricEn();
					lycics[1]= lyricLoaded.getLyricVi();   
				}
				else 
				{
					localSearch=2;
					try 
					{
						lycics = search.getLyricFromURL(urlSong);
						Log.d("tim loi dich tren web", "tim xong");
						
					} catch (Exception e) {
						// TODO: handle exception
					}
				}
				
			
			
			}
		}
		else 
		{


			localSearch=2;
			try 
			{
				lycics = search.getLyricFromURL(urlSong);
				Log.d("tim loi dich tren web", "tim xong");
				
			} catch (Exception e) {
				// TODO: handle exception
			}
		
		
		}
		
	}
	else if(songname!= null)
	{
		lyricLoaded = data.getLyricByName(songname,singer);
		if(lyricLoaded!=null)
		{
			
			lycics = new String [2];
			lycics[0]= lyricLoaded.getLyricEn();
			lycics[1]= lyricLoaded.getLyricVi();
			
		}
		else 
		{


			
			lyricLoaded = data.getLyricFromHost(songname,singer);
			if(lyricLoaded!= null)
			{
				localSearch=1;
				Log.d("LoadLyricFromHost", "have lyric from host");
				lycics = new String [2];
				lycics[0]= lyricLoaded.getLyricEn();
				lycics[1]= lyricLoaded.getLyricVi(); 
			}
			else 
			{
				localSearch = 2;
				try 
				{
					//lycics = search.getLyric(songname);
					//if(singer.toLowerCase().indexOf("unknow")<0)
					//{
						Log.d("search web", "gui ten ca sy "+ singer);
						lycics = search.getLyricByName(songname,singer);//gui ten ca si len
					//}
					//else 
						//lycics = search.getLyric(songname);
					Log.d("tim loi dich tren web", "tim xong");
					
				} catch (Exception e) {
					// TODO: handle exception
				}
			}
			
		
		
		}
		
	}*/
	
	
	
	
	

	

}
